﻿using System;
using System.Collections.Generic;
using System.Text;
using Expo.Training.Entity;
using Expo.Training.CustomerData;
using System.Data.SqlClient;
using System.Data;
using Expo.Training.DataAccess.Course;

namespace Expo.Training.DataAccess.Special
{
    public class SpecialManager
    {
        /// <summary>
        /// 获取专项推送列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <param name="sort"></param>
        /// <param name="title"></param>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public static DataTable GetSpecialList(int pageIndex, int pageSize, out int totalCount, string sort, string title, DateTime beginDate, DateTime endDate)
        {
            StringBuilder searchCondition = new StringBuilder();
            searchCondition.Append(" 1=1 ");
            if (beginDate != DateTime.Parse("1900-1-1"))
            {
                searchCondition.Append("AND AddTime >= '" + beginDate + "' ");
            }
            if (endDate != DateTime.Parse("1900-1-1"))
            {
                searchCondition.Append("AND AddTime <= '" + endDate + "' ");
            }
            if (title != null && title.Length > 0)
            {
                searchCondition.Append("AND SpecialTitle LIKE '%" + title + "%'");
            }
            Common objComon = new Common();
            return objComon.GetRecordFromPage("Special_Info", searchCondition.ToString(), sort, pageIndex, pageSize, out totalCount, Database.DBType.DB);

        }

        /// <summary>
        /// 按查询条件获取专项推送列表
        /// </summary>
        public static DataTable GetSpecialList(string queryExpression, int pageIndex, int pageSize, out int totalCount, string sort, string title, DateTime beginDate, DateTime endDate)
        {
            StringBuilder searchCondition = new StringBuilder();
            searchCondition.Append(" 1=1 ");
            if (beginDate != DateTime.Parse("1900-1-1"))
            {
                searchCondition.Append("AND AddTime >= '" + beginDate + "' ");
            }
            if (endDate != DateTime.Parse("1900-1-1"))
            {
                searchCondition.Append("AND AddTime <= '" + endDate + "' ");
            }
            if (title != null && title.Length > 0)
            {
                searchCondition.Append("AND SpecialTitle LIKE '%" + title + "%'");
            }
            if (!string.IsNullOrEmpty(queryExpression))
            {
                searchCondition.Append("AND " + queryExpression);
            }

            Common objComon = new Common();
            return objComon.GetRecordFromPage("Special_Info", searchCondition.ToString(), sort, pageIndex, pageSize, out totalCount, Database.DBType.DB);

        }

        /// <summary>
        /// 获取专项推送列表(置顶6条)
        /// </summary>
        /// <param name="sort"></param>
        /// <returns></returns>
        public static DataTable GetSpecialList(string sort)
        {
            int totalCount = 0;
            Common objComon = new Common();
            return objComon.GetRecordFromPage("Special_Info", "", sort, 1, 6, out totalCount, Database.DBType.DB);
        }

        /// <summary>
        /// 获取巴马专项推送列表(置顶6条)
        /// </summary>
        /// <param name="sort"></param>
        /// <returns></returns>
        public static DataTable GetSpecialListForBM(string sort)
        {
            int totalCount = 0;
            Common objComon = new Common();
            return objComon.GetRecordFromPage("Special_Info", "Number > 100", sort, 1, 6, out totalCount, Database.DBType.DB);
        }

        /// <summary>
        /// 获取专项推送课程列表
        /// </summary>
        /// <param name="SpecialID"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public static DataTable GetSpecialCourseList(string SpecialID,string sort)
        {
            int totalCount = 0;
            StringBuilder searchCondition = new StringBuilder();
            searchCondition.Append(" 1=1 ");
            if (SpecialID != "")
            {
                searchCondition.Append("AND SpecialID = '" + SpecialID + "' ");
            }
            Common objComon = new Common();
            return objComon.GetRecordFromPage("vw_SpecialCourseList", searchCondition.ToString(), sort, 1, 200, out totalCount, Database.DBType.DB);
        }

        /// <summary>
        /// [巴马专用]获取专项推送课程列表
        /// </summary>
        /// <param name="SpecialID"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public static DataTable GetSpecialCourseListForBM(string SpecialID, string sort)
        {
            int totalCount = 0;
            StringBuilder searchCondition = new StringBuilder();
            searchCondition.Append(" 1=1 ");
            if (SpecialID != "")
            {
                searchCondition.Append("AND SpecialID = '" + SpecialID + "' ");
            }
            Common objComon = new Common();
            return objComon.GetRecordFromPage("vw_SpecialCourseList_BM", searchCondition.ToString(), sort, 1, 200, out totalCount, Database.DBType.DB);
        }

        /// <summary>
        /// 13.1【慈溪专用】获取专项推送列表
        /// </summary>
        /// <param name="CustomerID"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public static List<SpecialMobileList> GetSpecialListFor99XB(Guid CustomerID, int PageIndex, int PageSize)
        {
            string HttpUrl = string.Format("{0}", System.Configuration.ConfigurationManager.AppSettings.Get("HttpUrl"));
            List<SpecialMobileList> specialList = new List<SpecialMobileList>();
            Database objDB = new Database();
            SqlDataReader objReader = null;
            try
            {
                SqlParameter[] prams = {
                    objDB.MakeInParam("@CustomerID", System.Data.SqlDbType.UniqueIdentifier, 36, CustomerID),
                    objDB.MakeInParam("@PageIndex", System.Data.SqlDbType.Int, 4, PageIndex),
                    objDB.MakeInParam("@PageSize", System.Data.SqlDbType.Int, 4, PageSize)
                };
                objDB.RunProc("sp_Get_SpecialListFor99XB", prams, out objReader);
                SpecialMobileList specialItem = null;
                while (objReader.Read())
                {
                    specialItem = new SpecialMobileList();
                    specialItem.SpecialID = new Guid(string.Format("{0}", objReader["SpecialID"]));
                    specialItem.SpecialTitle = string.Format("{0}", objReader["SpecialTitle"]);
                    specialItem.SpecialContent = string.Format("{0}", objReader["SpecialContent"]);
                    string _ImageUrl = string.Format("{0}", objReader["ImageUrl"]);
                    if (_ImageUrl.Length > 0)
                    {
                        _ImageUrl = string.Format("{0}Tako/Data/Special/{1}", HttpUrl, _ImageUrl);
                    }
                    else
                    {
                        _ImageUrl = string.Empty;
                    }
                    specialItem.ImageUrl = _ImageUrl;
                    string _HomePageImageUrl = string.Format("{0}", objReader["HomePageImageUrl"]);
                    if (_HomePageImageUrl.Length > 0)
                    {
                        _HomePageImageUrl = string.Format("{0}Tako/Data/Special/HomePage/{1}", HttpUrl, _HomePageImageUrl);
                    }
                    else
                    {
                        _HomePageImageUrl = string.Empty;
                    }
                    specialItem.HomePageImageUrl = _HomePageImageUrl;
                    specialItem.AddTime = Convert.ToDateTime(string.Format("{0}", objReader["AddTime"])).ToString("yyyy-MM-dd HH:mm:ss");
                    specialList.Add(specialItem);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (objReader != null)
                {
                    objReader.Close();
                    objReader.Dispose();
                }
                objDB.Close();
                objDB.Dispose();
            }
            return specialList;
        }

        /// <summary>
        /// 13.2【慈溪专用】获取专项推送详细信息
        /// </summary>
        /// <param name="CustomerID"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public static SpecialMobileInfo GetSpecialInfoFor99XB(Guid MemberID, Guid SpecialID)
        {
            string HttpUrl = string.Format("{0}", System.Configuration.ConfigurationManager.AppSettings.Get("HttpUrl"));
            SpecialMobileInfo specialInfo = null;

            Special_Info objSpecial = new Special_Info();
            objSpecial.SpecialID = SpecialID;
            if (objSpecial.Exist() == true)
            {
                specialInfo = new SpecialMobileInfo();
                specialInfo.SpecialID = objSpecial.SpecialID;
                specialInfo.SpecialTitle = objSpecial.SpecialTitle;
                specialInfo.SpecialContent = objSpecial.SpecialContent;
                string _ImageUrl = objSpecial.ImageUrl;
                if (_ImageUrl.Length > 0)
                {
                    _ImageUrl = string.Format("{0}Tako/Data/Special/{1}", HttpUrl, _ImageUrl);
                }
                else
                {
                    _ImageUrl = string.Empty;
                }
                specialInfo.ImageUrl = _ImageUrl;
                string _HomePageImageUrl = objSpecial.HomePageImageUrl;
                if (_HomePageImageUrl.Length > 0)
                {
                    _HomePageImageUrl = string.Format("{0}Tako/Data/Special/HomePage/{1}", HttpUrl, _HomePageImageUrl);
                }
                else
                {
                    _HomePageImageUrl = string.Empty;
                }
                specialInfo.HomePageImageUrl = _HomePageImageUrl;
                specialInfo.IsTop = objSpecial.IsTop;
                specialInfo.AddTime = objSpecial.AddTime.ToString("yyyy-MM-dd HH:mm:ss");

                Database objDB = new Database();
                SqlDataReader objReader = null;
                SqlParameter[] prams = {
                    objDB.MakeInParam("@MemberID", System.Data.SqlDbType.UniqueIdentifier, 36, MemberID),
                    objDB.MakeInParam("@SpecialID", System.Data.SqlDbType.UniqueIdentifier, 36, SpecialID)
                };
                objDB.RunProc("sp_Get_GetSpecialCourseFor99XB", prams, out objReader);
                List<MemberMobileCourse> objSpecialCourseList = new List<MemberMobileCourse>();
                while (objReader.Read())
                {
                    MemberMobileCourse specialCourseItem = new MemberMobileCourse();
                    specialCourseItem.CourseID = new Guid(string.Format("{0}", objReader["CourseID"]));
                    specialCourseItem.CourseName = string.Format("{0}", objReader["CourseName"]);
                    specialCourseItem.AddTime = DateTime.Parse(string.Format("{0}", objReader["AddTime"])).ToString("yyyy-MM-dd HH:mm:ss");
                    string _CoursePicture = string.Format("{0}", objReader["CoursePicture"]);
                    if (_CoursePicture.Length > 0)
                    {
                        _CoursePicture = string.Format("{0}Data/CoursePicture/{1}", HttpUrl, _CoursePicture);
                    }
                    else
                    {
                        _CoursePicture = string.Empty;
                    }
                    specialCourseItem.CoursePicture = _CoursePicture;
                    specialCourseItem.SupportMobile = bool.Parse(string.Format("{0}", objReader["SupportMobile"]));
                    specialCourseItem.StudyCount = int.Parse(string.Format("{0}", objReader["StudyCount"]));
                    specialCourseItem.CommentAvgScore = string.Format("{0}", objReader["CommentAvgScore"]);
                    specialCourseItem.IsLastest = int.Parse(string.Format("{0}", objReader["IsLastest"]));
                    specialCourseItem.IsRecommend = int.Parse(string.Format("{0}", objReader["IsRecommend"]));
                    specialCourseItem.Progress = int.Parse(string.Format("{0}", objReader["Progress"]));
                    specialCourseItem.IsPass = bool.Parse(string.Format("{0}", objReader["IsPass"]));
                    specialCourseItem.IsTop = int.Parse(string.Format("{0}", objReader["IsTop"]));
                    objSpecialCourseList.Add(specialCourseItem);
                }

                specialInfo.CourseInfo = objSpecialCourseList;
            }
            return specialInfo;
        }
    }
}
